<?php

namespace app\admin\controller\dealer;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="审批列表")
 */
class Examine extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\company\Identity();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->where('type',4)
                ->count();
            if ($count <1) {
                $list = '';
            } else {
                $list = $this->model
                ->where($where)
                ->where('type',4)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                $identityDealer = $this->model->where('type',2)->where('status',1)->find();
                foreach ($list as $key => &$value) {
                    $value['idntity_dealer'] = $identityDealer->name; //经销商名
                }
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->layoutBgColor();
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="审批")
     */
    public function examine()
    {
        $param = $this->request->param();
        $row = $this->model->find($param['order_id']);
        empty($row) && $this->error('数据不存在');
        
        try {
            // examine_status 审批状态 {select}  (0:待审批, 1:通过 2:驳回)
            if ($param['type'] == 1) {
                $row->examine_status = 1;
                $row->display_status = 1;
                $row->update_time = time();
            } else {
                $row->examine_status = 2;
                $row->display_status = 0;
                $row->update_time = time();
            }
            $save = $row->save();
        } catch (\Exception $e) {
            $this->error('操作失败');
        }
        $save ? $this->success('操作成功') : $this->error('操作失败');
    }
    /**
     * @NodeAnotation(title="审核驳回")
     */
    public function examine_reject($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['examine_status'] = 2;
                $post['display_status'] = 0;
                $post['update_time'] = time();
                $post['examine_content'] = $post['examine_content'] ??'';
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }
}